# dataset fashion MNIST
import tensorflow as tf

print(tf.__version__)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()
print(train_image.shape, train_label.shape)
# plt.imshow(train_image[0])
# plt.show()
print(np.max(train_image[0]))
# print(train_label)
train_image = train_image/255
test_image = test_image/255
#
# model = tf.keras.Sequential()
# model.add(
#     tf.keras.layers.Flatten(input_shape=(28,28)), # 展宽
#
#
# )
# model.add(
#     tf.keras.layers.Dense(128, activation='relu')
# )
# model.add(tf.keras.layers.Dense(10, activation='softmax'))
# model.compile(optimizer='adam',
#               loss='sparse_categorical_crossentropy',
#               metrics=['acc'])
#
# model.fit(train_image,train_label,epochs=5)
# evaluate = model.evaluate(test_image, test_label)
# print(evaluate)
# # When to use cat

train_label_onehot = tf.keras.utils.to_categorical(train_label)
# print(train_label_onehot)

test_label_onehot = tf.keras.utils.to_categorical(test_label)
# print(test_label_onehot)

model = tf.keras.Sequential()
model.add(
    tf.keras.layers.Flatten(input_shape=(28,28)) # 展宽
)
model.add(
    tf.keras.layers.Dense(128, activation='relu')
)
model.add(tf.keras.layers.Dropout(0.5))
model.add(
    tf.keras.layers.Dense(128, activation='relu')
)
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              loss='categorical_crossentropy',
              metrics=['acc'])

history = model.fit(train_image,train_label_onehot,epochs=10,
                    validation_data=(test_image, test_label_onehot))
# evaluate = model.evaluate(test_image, test_label_onehot)
# print(evaluate)

print(history.history.keys())
plt.plot(history.epoch, history.history.get("loss"), label="loss")
plt.plot(history.epoch, history.history.get("val_loss"), label="val_loss")
plt.legend()
plt.show()